﻿using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace Grinder.Renamer.RenamePlugins
{
    internal sealed class AudioRenamePlugin : IRenamePlugin
    {
        private readonly Dictionary<string, string> _tokens;

        public IEnumerable<string> Extensions
        {
            get
            {
                return new List<string> {
                        "*.MP3", 
                        "*.FLAC", 
                        "*.OGG", 
                        "*.WMA",
                        "*.APE",
                        "*.AAC",                        
                };
            }
        }
        public string Name { get { return "Audio Files"; } }
        public Dictionary<string, string> Tokens { get { return _tokens; } }
        public Image Icon { get { return Properties.Resources.music; } }
        public override string ToString() { return string.Format("{0} ({1})", Name, string.Join(",", Extensions.ToArray()).ToLower()); }

        public AudioRenamePlugin()
        {
            _tokens = new Dictionary<string, string>
                          {
                             {"album", "Album name"},
                             {"comment", "Comment"},
                             {"conductor", "Conductor"},
                             {"disc", "Disc"},
                             {"disccount", "Number of disks"},
                             {"albumartist", "Album artist"},
                             {"artist", "Artist"},
                             {"composer", "Composer"},
                             {"genre", "Genre"},
                             {"year", "Year"},
                             {"title", "Title"},
                             {"track", "Track"},
                             {"trackcount", "Number of tracks"},
                             {"bitrate", "Bitrate"},
                             {"duration", "Duration"}
                          };
            TagLib.ByteVector.UseBrokenLatin1Behavior = true;
        }

        public Dictionary<string, object> GetTokens(FileInfo file)
        {
            var audioFile = TagLib.File.Create(file.FullName);
            var dic = new Dictionary<string, object>
                          {
                              {"album", audioFile.Tag.Album},
                              {"comment", audioFile.Tag.Comment},
                              {"conductor", audioFile.Tag.Conductor},
                              {"disc", audioFile.Tag.Disc},
                              {"disccount", audioFile.Tag.DiscCount},
                              {"albumartist", audioFile.Tag.JoinedAlbumArtists},
                              {"artist", audioFile.Tag.JoinedPerformers},
                              {"composer", audioFile.Tag.JoinedComposers},
                              {"genre", audioFile.Tag.JoinedGenres},
                              {"year", audioFile.Tag.Year},
                              {"title", audioFile.Tag.Title},
                              {"track", audioFile.Tag.Track},
                              {"trackcount", audioFile.Tag.TrackCount},
                              {"bitrate", audioFile.Properties.AudioBitrate},
                              {"duration", string.Format("{0}.{1}.{2}",
                                                            audioFile.Properties.Duration.Hours,
                                                            audioFile.Properties.Duration.Minutes,
                                                            audioFile.Properties.Duration.Seconds)}
                          };

            return dic;
        }
    }
}

